import React, { useState } from "react";
import { Button, message } from "antd";
import useData from "@/store";
import api from "@/fetch";
import "./index.less";

const Super: React.FC = () => {
  const [isLoading, setIsLoading] = useState(false);
  const [isLoadingRes, setIsLoadingRes] = useState(false);
  const handleApply = async () => {
    setIsLoading(true);

    try {
      await api.post("/api/v1/role/apply");
      message.success("申请已发出");
    } catch (error) {
      message.error(error?.message || "申请超管失败");
    } finally {
      setIsLoading(false);
    }
  };

  const handleDeal = async (result: number) => {
    setIsLoadingRes(true);

    try {
      await api.post("/api/v1/admin/role/review", {
        userId: useData.getState().userInfo?.userId,
        reviewResult: result,
      });
      message.success("操作成功");
    } catch (error) {
      message.error(error?.message || "审核失败");
    } finally {
      setIsLoadingRes(false);
    }
  };

  return (
    <div className="super-container">
      <Button type="primary" onClick={handleApply} loading={isLoading}>
        申请成为超管
      </Button>
      <Button danger onClick={() => handleDeal(2)} loading={isLoadingRes}>
        拒绝
      </Button>
      <Button
        type="primary"
        onClick={() => handleDeal(1)}
        loading={isLoadingRes}
      >
        通过
      </Button>
    </div>
  );
};

export default Super;
